package com.example.x.dnuifood_929dx.adapter;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.x.dnuifood_929dx.R;
import com.example.x.dnuifood_929dx.bean.CommentInfo;
import com.example.x.dnuifood_929dx.bean.ResultBean;
import com.example.x.dnuifood_929dx.controller.MyCommentActivity;
import com.example.x.dnuifood_929dx.listener.onRetrofitListener;
import com.example.x.dnuifood_929dx.model.FoodModel;

import java.util.List;

/**
 * Created by X on 2018/12/4.
 */

public class OrderAdapter extends  RecyclerView.Adapter<RecyclerView.ViewHolder> implements onRetrofitListener<ResultBean> {
    private FoodModel foodModel;
    private List<CommentInfo>list;
    private Context context;
    private LayoutInflater layoutInflater;


    public OrderAdapter(Context context, List list, int layoutResource) {
        this.context=context;
        this.list=list;
        layoutInflater = LayoutInflater.from(context);
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View v = layoutInflater.from(viewGroup.getContext()).inflate(R.layout.shop_item5, viewGroup, false);
        return new OrderAdapter.ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        final CommentInfo entity = (CommentInfo)list.get(position);

        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.myorder_orderid.setText(entity.getOrde_id());
        viewHolder.myorder_shopname.setText(entity.getShopname());
        viewHolder.myorder_ordertime.setText(entity.getOrder_time());
        viewHolder.myorder_foodname.setText(entity.getFoodname());
        viewHolder.myorder_sum.setText("¥" + entity.getSum());
        if(entity.getContent()!=null&&entity.getContent()==null){
            viewHolder.myorder_comment.setVisibility(View.GONE); }
        viewHolder.myorder_comment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final EditText et = new EditText(context);
                new AlertDialog.Builder(context).setTitle("评论") .setView(et)
                        .setPositiveButton("确定",new DialogInterface.OnClickListener(){ public void onClick(DialogInterface dialog, int which) {
                            doComment(entity.getItem_id(),et.getText().toString()); }
                        }).setNegativeButton("取消",null) .show();
            } });
    }
    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView myorder_shopname, myorder_ordertime, myorder_foodname, myorder_sum,
                myorder_orderid;
        Button myorder_comment;
        public ViewHolder(View itemView) {
            super(itemView);
            myorder_orderid = (TextView) itemView.findViewById(R.id.myorder_orderid);
            myorder_shopname = (TextView) itemView.findViewById(R.id.myorder_shopname);
            myorder_ordertime = (TextView) itemView.findViewById(R.id.myorder_ordertime);
            myorder_foodname = (TextView) itemView.findViewById(R.id.myorder_foodname);
            myorder_sum = (TextView) itemView.findViewById(R.id.myorder_sum);
            myorder_comment = (Button) itemView.findViewById(R.id.myorder_comment);
        }
    }


    private void doComment(String item_id,String et){
        foodModel=new FoodModel();
        foodModel.insertComment(item_id,et,this);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }



    @Override
    public void onSuccess(ResultBean object, int flag) {

        if(object.getSuccess().equals("1")){
            Intent intent=new Intent(context,MyCommentActivity.class);
            context.startActivity(intent);
            Toast.makeText(context,"评论成功", Toast.LENGTH_SHORT).show();
        }else{
            Toast.makeText(context,"评论失败", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onFailure(String msg) {

    }

}